//
//  NetViewController.m
//  UIComprehensive
//
//  Created by lanou3g on 16/1/28.
//  Copyright © 2016年 徐强. All rights reserved.
//

#import "NetViewController.h"
#import "NewsModel.h"
#define NEWSURL @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"
#define NEWSPRE @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"
#define NEWSLAST @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"


@interface NetViewController ()<NSURLConnectionDataDelegate>
{
    //    Get异步代理形式中用
    NSMutableData *datas;
}
@end

@implementation NetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"网络解析";
}
#pragma mark - GET同步
- (IBAction)GETtongbu:(id)sender {
    //同步建立连接请求数据
    NSData *data = [NSURLConnection sendSynchronousRequest:[NSMutableURLRequest requestWithURL:[NSURL URLWithString:NEWSURL]] returningResponse:nil error:nil];
    //解析
    if (data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        NSMutableArray *array = [NSMutableArray array];
        //遍历news中的数组
        for (NSDictionary *dicTemp in dic[@"news"]) {
            NewsModel *model = [[NewsModel alloc]init];
            //KVC
            [model setValuesForKeysWithDictionary:dicTemp];
            [array addObject:model];
        }
        NSLog(@"---------------GET同步---------------");
        NSLog(@"GET同步:%@",array);
    }
}
#pragma mark - Get异步block形式
- (IBAction)GETyibu:(id)sender {
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:NEWSURL]];
    //一步建立连接
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if (data){
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSMutableArray *array = [[NSMutableArray alloc]init];
            // 遍历news中的数组
            for (NSDictionary *dicTemp in dic[@"news"]) {
                NewsModel *model = [[NewsModel alloc]init];
                //KVC
                [model setValuesForKeysWithDictionary:dicTemp];
                [array addObject:model];
            }
            NSLog(@"---------------Get异步block---------------");

            NSLog(@"Get异步block:%@",array);
        }
    }];
}
#pragma mark - Get异步代理形式
- (IBAction)GETyibuDelegate:(id)sender {
    
    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:[NSMutableURLRequest requestWithURL:[NSURL URLWithString:NEWSURL]] delegate:self];
    [connection start];
}
#warning 代理方法
//接收到响应时调用的方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    datas = [NSMutableData data];
}
//接收数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    //将每次拿到的数据拼接在datas中
    [datas appendData:data];
}
//结束下载
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    //将所有的数据在这里使用
    NSLog(@"---------------Get异步代理---------------");
    
    NSLog(@"Get异步代理:%@",datas);
}

#pragma mark - POST同步
- (IBAction)POSTtongbu:(id)sender {
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:NEWSPRE]];
    //注意设置请求方法为“post”，默认为get
    [request setHTTPMethod:@"POST"];
    //设置包体(body)
    [request setHTTPBody:[NEWSLAST dataUsingEncoding:NSUTF8StringEncoding]];
    //建立连接
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:0 error:nil];
    if (data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSLog(@"%@",dic);
        NSMutableArray *arrAllData = [NSMutableArray array];
        for (NSDictionary *dicTemp in dic[@"news"]) {
            NewsModel *model = [[NewsModel alloc]init];
            [model setValuesForKeysWithDictionary:dicTemp];
            [arrAllData addObject:model];
        }
        NSLog(@"---------------POST同步---------------");
        
        NSLog(@"POST同步:%@",arrAllData);
    }
    
    
    
}
#pragma mark - session做网络请求
- (IBAction)NSURLSession:(id)sender {
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:[NSMutableURLRequest requestWithURL:[NSURL URLWithString:NEWSURL]] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSLog(@"---------session做网络请求---------");
            NSLog(@"session:%@",dic);
        }
    }];
    //类似于开始任务
    [task resume];
}

@end
